package com.tx.weixin.controller;


import com.tx.weixin.util.CheckUtil;
import com.tx.weixin.util.XmlUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Map;

/**
 * 接入微信公众平台开发，开发者需要按照如下步骤完成：
 * <p>
 * 1、填写服务器配置
 * <p>
 * 2、验证服务器地址的有效性
 * <p>
 * 3、任何微信动作，都会以post请求通知到该地址
 */
@RestController
public class WxServlet {

    /**
     *  验证签名接入
     * @param signature
     * @param timestamp
     * @param nonce
     * @param echostr
     * @return
     */
    //signature	微信加密签名，timestamp时间戳,nonce	随机数,echostr	随机字符串
    @GetMapping("/wx")
    public String doGet(String signature,
                                       String timestamp,
                                       String nonce,
                                       String echostr) {
        //验证失败
        if (!CheckUtil.checkSignature(signature, timestamp, nonce)) {
            return null;
        }
        //成功返回参数
        return echostr;
    }


    /**
     *  接收消息和事件推送
     * @param request
     * @param response
     * @throws Exception
     */
    @PostMapping("/wx")
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");

        //处理消息和事件推送
        Map<String, String> requestMap = XmlUtils.parseXml(request);
        System.out.println(requestMap);



        //准备回复的数据包
//        String resXml="<xml><ToUserName><![CDATA["+toUserName+"]]></ToUserName><FromUserName><![CDATA["+fromUserName+"]]></FromUserName><CreateTime>"+currentTime/1000+"</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[what?]]></Content></xml>";
        String resXml=XmlUtils.getRespose(requestMap);
        System.out.println(resXml);

        PrintWriter out = response.getWriter();
        out.println(resXml);
        out.flush();
        out.close();



//        switch (msgType) {
//            case "text":
//                String resultXml = null;
//                PrintWriter out = response.getWriter();
//
//                TextMessage text = new TextMessage();
//                text.setToUserName(toUserName);
//                text.setFromUserName(fromUserName);
//                text.setCreateTime(System.currentTimeMillis()/1000);
//                text.setMsgType("text");
//                if (content.equals("1")) {
//                    text.setContent("感谢关注，有什么可以帮到您的？");
//
//                } else {
//                    String resultApi = HttpClientUtil.doGet("http://api.qingyunke.com/api.php?key=free&appid=0&msg=" + content);
//                    JSONObject jsonObject = JSON.parseObject(resultApi);
//                    Integer state = jsonObject.getInteger("result");
//                    if (state != null) {
//                        String string = jsonObject.getString("content");
//                        text.setContent(string);
//                    }
//                }
//                resultXml = XmlUtils.messageToXml(text);
//                out.println(resultXml);
//                out.flush();
//                out.close();
//                break;
//            default:
//                break;
//        }

    }
}
